package com.tangmu.rabbits.adapter.optimization;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.tangmu.rabbits.R;
import com.tangmu.rabbits.bean.Screen;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/4/27.
 */

public class ClassifyScreenAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private LayoutInflater inflater;
    private List<Screen> datas = new ArrayList<>();

    public ClassifyScreenAdapter(Context mContext, List<Screen> datas) {
        this.mContext = mContext;
        this.datas = datas;
        inflater = LayoutInflater.from(mContext);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ScreenViewHolder(inflater.inflate(R.layout.rv_item_screen_layout, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        ScreenViewHolder viewHolder = (ScreenViewHolder) holder;
        Screen screen = datas.get(position);
        viewHolder.tvName.setText("" + screen.getName());
        if (screen.isChoosed()) {
            viewHolder.tvName.setBackgroundResource(R.drawable.tv_scrren_item_select_bg);
        } else {
            viewHolder.tvName.setBackgroundResource(R.drawable.tv_scrren_item_normal_bg);
        }
        viewHolder.tvName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (Screen sc : datas) {
                    sc.setChoosed(false);
                }
                datas.get(position).setChoosed(true);
                notifyDataSetChanged();
            }
        });
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    class ScreenViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_screen_name)
        TextView tvName;

        public ScreenViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


}
